package com.newasia.server.serviceImpl.manage;

import com.newasia.server.common.template.ModuleAnalysisTemplate;
import com.newasia.server.common.template.dataItem.basics.CurrentStatus;
import com.newasia.server.common.template.dataItem.basics.DataType;
import com.newasia.server.dal.entitys.returnValue.MonitorState;
import jodd.util.StringUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

import java.util.*;

/**
 * @author LiChong
 * @description 处理得到的监控点状态
 * @date 2020/4/23
 */

@Data
public class MonitorStateManage {

    private String monitorName;

    private String sid;

    private Long monitorId;

    private boolean isTop;

    private String lon;

    private String lat;

    private Date stopTime;

    private Date addTime;

    private Boolean position;

    private DataType showData;

    private CurrentStatus status;

    public MonitorStateManage(MonitorState monitorState){

        this.setMonitorName(monitorState.getMonitorName());;
        this.setIsTop(monitorState.getIsTop());
        this.setStopTime(monitorState.getStopTime());
        this.setAddTime(monitorState.getAddTime());
        this.setLatAndLon(monitorState.getLat(),monitorState.getLon());
        this.setSid(monitorState.getSid());
        this.setMonitorId(monitorState.getMonitorId());

        ModuleAnalysisTemplate template = monitorState.getTemplate();
        this.setShowData(template.getShowData());
        //检测是否离线
        CurrentStatus currentStatus = new CurrentStatus();
        boolean flagOffline = true;
        try {
            Date upload_time = monitorState.getUploadTime();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(upload_time);
            calendar.set(Calendar.MINUTE,
                    calendar.get(Calendar.MINUTE) + 20);
            upload_time = calendar.getTime();
            Date date = new Date();
            if (upload_time.getTime() > date.getTime()) {
                flagOffline = false;
            }
        }
        catch(Exception e1){
            e1.printStackTrace();
        }
        if (!flagOffline) {
            if (monitorState.getFlagAlert()) {
                currentStatus.setStateFlag(1);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("key", 1);
                map.put("value", "告警中");
                currentStatus.setState(map);
            }else
                currentStatus = template.getCurrentStatus();
        }else {
            currentStatus.setStateFlag(-1);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("key",-1);
            map.put("value","离线中");
            currentStatus.setState(map);
        }
        this.setStatus(currentStatus);
    }

    /**
     *是否过期
     * @author LiChong
     * @date 2020/4/28
     * @param  * @param
     * @return boolean
     */
    public void setStatus(CurrentStatus status){
        this.status = status;
        try {
            if (stopTime!=null&&(stopTime.getTime() - new Date().getTime() < 0)) {
                this.status.setStateFlag(-2);
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("key",-2);
                map.put("value","欠费");
                this.status.setState(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *定位
     * @author LiChong
     * @date 2020/4/28
     * @param  * @param stopTime
     * @return void
     */
    public void setLatAndLon(String lat,String lon){
        this.lat = lat;
        this.lon = lon;
        if (!StringUtil.isEmpty(lat)&&!StringUtil.isEmpty(lon))
           this.position = true;
        else
            this.position = false;
    }

    public void setIsTop(Integer isTop){
        this.isTop = true;
        if(isTop==null||isTop==0){
            this.isTop = false;
        }
    }
}
